package com.ssy.lingxi.component.redis.config;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * Redis集群模式判断
 * @author 万宁
 * @version 2.0.0
 * @date 2021-06-18
 */
@Component
public class RedisModeDetector {

    @Resource
    private Environment environment;

    /**
     * 使用内存变量，缓存是否集群定义。避免每次都从Enviroment中查询
     */
    private Boolean clusterFlag;

    /**
     * 根据application.yml文件中是否存在Redis集群节点的配置，来判断是否集群模式
     * @return Cluster模式返回true
     */
    public boolean isCluster() {
        if(clusterFlag != null) {
            return clusterFlag;
        }

        clusterFlag = StringUtils.hasLength(environment.getProperty("spring.redis.cluster.nodes",""));
        return clusterFlag;
    }
}
